package ast;

import parser.BinaryOperatorType;
import lexer.Position;

public class BinaryOperatorNode extends ExpressionNode {
	
	public ExpressionNode getLeft() {
		return left;
	}

	public ExpressionNode getRight() {
		return right;
	}

	public BinaryOperatorType getOp() {
		return op;
	}

	private final BinaryOperatorType op;
	private final ExpressionNode left;
	private final ExpressionNode right;

	public BinaryOperatorNode(Position position, ExpressionNode left, ExpressionNode right, BinaryOperatorType op) {
		super(position, left, right);
		this.op = op;
		this.left = left;
		this.right = right;
	}

	@Override
	public String toString() {
		return "BinaryOperatorNode [op=" + op + ", left=" + left + ", right="
				+ right + "]";
	}
	
	public long collapseExp() throws Exception {
		switch (this.op) {
		case TIMES: return this.left.collapseExp() * this.right.collapseExp();
		case PLUS:  return this.left.collapseExp() + this.right.collapseExp();
		case DIVIDE: return this.left.collapseExp() / this.right.collapseExp();
		case MINUS: return this.left.collapseExp() - this.right.collapseExp();
		case MODULO: return this.left.collapseExp() % this.right.collapseExp();
		default: throw new Exception("not recognized operator: " + op.toString());
		}
	}

}
